﻿using AbpApplication.Expand;
using AbpApplication.InPut;
using AbpToDomain.EntityInfo;
using Castle.Core.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace AbpApplication.AppServices
{
    
    public class EssService : ApplicationService, IEssService
    {
        private readonly IRepository<TaskOrder> TaskOrder_res;
        private readonly IRepository<Box> box_res;
        private readonly IRepository<WarehouseLine> warehouse_line_res;
        private readonly ILoginHelper loginHelper;
        private readonly IBaseService baseService;

        public EssService(IRepository<TaskOrder> taskOrder_res, IRepository<Box> box_res, IRepository<WarehouseLine> warehouse_line_res, ILoginHelper loginHelper, IBaseService baseService)
        {
            TaskOrder_res = taskOrder_res;
            this.box_res = box_res;
            this.warehouse_line_res = warehouse_line_res;
            this.loginHelper = loginHelper;
            this.baseService = baseService;
        }
        public async Task<ApiResponse> AbnormalCapture(InPut_EssService2 InPut)
        {
            return new ApiResponse("ok", true, null);
        }//异常回调
        public async Task<ApiResponse> TaskStatusChange(InPut_EssService InPut)
        {
            try
            {            
                if (InPut.eventType == "task")
                {
                    if (InPut.status == "success")
                    {
                        TaskOrder taskOrder = await TaskOrder_res.FirstOrDefaultAsync(x => x.TaskOrderCode == InPut.taskCode);
                        if (taskOrder == null)
                        {
                            loginHelper.LoginAsync(InPut, $"TaskStatusChange-未查询到改任务+{taskOrder.Id_Box + taskOrder.WarehouseCode + taskOrder.TaskOrderCode}");
                            return new ApiResponse("未查询到该任务", false, null);
                        }
                        if (taskOrder.Type == "出库")
                        {
                            Box box = await box_res.FirstOrDefaultAsync(x => x.Id_Box == taskOrder.Id_Box);
                            WarehouseLine warehouseLine = await warehouse_line_res.FirstOrDefaultAsync(x => x.WarehouseLineCode == taskOrder.WarehouseCode);
                            warehouseLine.IsOccupy = false;
                            box.WarehouseLine = null;
                            box.Id_WarehouseLine = null;
                            await box_res.UpdateAsync(box);
                            await warehouse_line_res.UpdateAsync(warehouseLine);
                            await TaskOrder_res.DeleteAsync(taskOrder);
                            return new ApiResponse("状态修改成功", true, null);
                        }
                        if (taskOrder.Type == "入库")
                        {
                            Box box = await box_res.FirstOrDefaultAsync(x => x.Id_Box == taskOrder.Id_Box);
                            WarehouseLine warehouseLine= await warehouse_line_res.FirstOrDefaultAsync(x => x.WarehouseLineCode == taskOrder.WarehouseCode);
                            box.Id_WarehouseLine = warehouseLine.Id_WarehouseLine;
                            box.WarehouseLine = warehouseLine;
                            await box_res.UpdateAsync(box);
                            await warehouse_line_res.UpdateAsync(warehouseLine);
                            await TaskOrder_res.DeleteAsync(taskOrder);
                            return new ApiResponse("状态修改成功", true, null);
                        }
                    }
                    if (InPut.status == "cancel"||InPut.status== "fail")
                    {
                        TaskOrder taskOrder = await TaskOrder_res.FirstOrDefaultAsync(x => x.TaskOrderCode == InPut.taskCode);
                        if (taskOrder == null)
                        {
                            return new ApiResponse("未查询到该任务", false, null);
                        }
                        if (taskOrder.Type == "入库")
                        {
                   
                            WarehouseLine warehouseLine = await warehouse_line_res.FirstOrDefaultAsync(x => x.WarehouseLineCode == taskOrder.WarehouseCode);
                            warehouseLine.IsOccupy=false;   
                            await warehouse_line_res.UpdateAsync(warehouseLine);
                            await TaskOrder_res.DeleteAsync(taskOrder);
                            loginHelper.LoginAsync(InPut, $"TaskStatusChange-任务失败/取消了+{taskOrder.Id_Box+taskOrder.WarehouseCode+taskOrder.TaskOrderCode}");
                            InPut_BaseService15 inPut = new InPut_BaseService15();
                            inPut.User = "ESS";
                            inPut.Note = "ESS失败/取消了任务" + InPut.taskCode + "msg：" + InPut.message + "事件类型：" + InPut.eventType + InPut.containerCode + InPut.isLocationHasContainer + InPut.stationCode + "状态：" + InPut.status;
                            inPut.Type = "异常";
                            await baseService.CreateLog(inPut);
                            return new ApiResponse("任务取消成功", true, null);
                        }
                    }
                }              
                return new ApiResponse("ok", true, null);
            }
            catch (Exception ex)
            {
                InPut_BaseService15 inPut = new InPut_BaseService15();
                inPut.User = "ESS";
                inPut.Note ="任务编号："+ InPut.taskCode+ "msg：" + InPut.message+ "事件类型："+InPut.eventType+InPut.containerCode+InPut.isLocationHasContainer+ InPut.stationCode+ "状态：" + InPut.status;
                inPut.Type = "异常";
                loginHelper.LoginAsync(InPut, $"TaskStatusChange任务回调-异常-{inPut.Note}");
                return new ApiResponse("内部错误服务器", false, null);
            }
           
        }//任务状态
    }
}
